﻿Imports BUS
Imports DTO
Public Class UserControlXoaNhanVien

    Private Sub btnXemDanhSach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXemDanhSach.Click
        LoadDanhSach()
    End Sub

    Private Sub LoadDanhSach()
        Dim nvBUS As New NhanVienBUS()
        Dim dt As New DataTable()
        lvwDanhSachPhieuGuiTien.Items.Clear()
        dt = nvBUS.LayDanhSachNhanVien()
        For i As Integer = 0 To dt.Rows.Count - 1
            Dim lv As ListViewItem = lvwDanhSachPhieuGuiTien.Items.Add(dt.Rows(i)("ID"))
            lv.SubItems.Add(dt(i)("IDNhanVien"))
            lv.SubItems.Add(dt(i)("TenNhanVien"))
            lv.SubItems.Add(dt(i)("TenChucVu"))
            lv.SubItems.Add(dt(i)("DiaChi"))
            lv.SubItems.Add(dt(i)("DienThoai"))
            lv.SubItems.Add(dt(i)("NgaySinh"))
        Next
    End Sub

    Private Sub lvwDanhSachPhieuGuiTien_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwDanhSachPhieuGuiTien.SelectedIndexChanged
        Dim index As Integer = lvwDanhSachPhieuGuiTien.Items(0).Index
        If index >= 0 Then
            txtIDNhanVien.Text = lvwDanhSachPhieuGuiTien.FocusedItem().SubItems(1).Text
        End If
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim MaID As String = txtIDNhanVien.Text
        Dim ID As Integer
        Dim nvBUS As New NhanVienBUS()
        For i As Integer = 0 To lvwDanhSachPhieuGuiTien.Items.Count - 1
            If txtIDNhanVien.Text = lvwDanhSachPhieuGuiTien.Items(i).SubItems(1).Text Then
                ID = Convert.ToInt32(lvwDanhSachPhieuGuiTien.Items(i).SubItems(0).Text)
            End If
        Next
        Dim dtDTO As New DataTable()
        dtDTO = nvBUS.KiemTraTonTaiNhanVien(txtIDNhanVien.Text)
        If dtDTO.Rows.Count = 0 Then
            MessageBox.Show("Nhân viên này không tồn tại")
            txtIDNhanVien.Focus()
            Return
        End If
        ID = Convert.ToInt32(dtDTO.Rows(0)("ID"))
        Dim kq As Integer = nvBUS.Xoa(ID)
        If kq <> 0 Then
            MessageBox.Show("Đã xóa nhân viên có mã IDNhanVien la" + txtIDNhanVien.Text)
            LoadDanhSach()
        Else
            MessageBox.Show("IDNhanVien không tồn tại")
        End If

    End Sub

End Class
